using System;
using System.IO;
using Serilog;
using Serilog.Events;

namespace ProgramLauncher.Utils
{
    public static class LogManager
    {
        private static readonly string LogPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "logs", "app.log");
        private static ILogger logger;

        static LogManager()
        {
            var logDir = Path.GetDirectoryName(LogPath);
            if (!Directory.Exists(logDir))
            {
                Directory.CreateDirectory(logDir);
            }

            logger = new LoggerConfiguration()
                .MinimumLevel.Debug()
                .WriteTo.File(
                    path: Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "logs", "app_.log"),
                    restrictedToMinimumLevel: LogEventLevel.Debug,
                    outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss.fff} [{Level:u3}] {Message:lj}{NewLine}{Exception}",
                    rollingInterval: RollingInterval.Day,
                    fileSizeLimitBytes: 10 * 1024 * 1024,
                    rollOnFileSizeLimit: true,
                    retainedFileCountLimit: 31)
                .CreateLogger();
        }

        public static void LogInfo(string message)
        {
            logger.Information(message);
        }

        public static void LogError(string message, Exception ex = null)
        {
            if (ex != null)
            {
                logger.Error(ex, message);
            }
            else
            {
                logger.Error(message);
            }
        }

        public static void LogWarning(string message)
        {
            logger.Warning(message);
        }

        public static void LogDebug(string message)
        {
            logger.Debug(message);
        }

        public static void Shutdown()
        {
            Log.CloseAndFlush();
        }
    }
} 